#include "HY1C_out.h"
/* ========================================================== */
/* rdsensorinfo() - reads parameters from MSL12 sensor files  */
/*                                                            */
/* Inputs:                                                    */
/*     sensorID - sensor ID as defined in l12_parms.h         */
/*     pname    - parameter name from sensor table            */
/*                                                            */
/* Outputs:                                                   */
/*     pval     - pointer to scalar or array containing param */
/*                                                            */
/* Written By:                                                */
/*     B. A. Franz, SAIC, NASA/SIMBIOS/SeaWiFS, October 2002  */
/*                                                            */
/* ========================================================== */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "l12_proto.h"
#include "config.h"

#define MAXBANDS (NBANDS+NBANDSIR)

void lowercase(char *s)
{
    while ((*s = (char) tolower((int)*s)))
        s++;
}

int32_t rdsensorinfo(int32_t   sensorID,
                  int32_t   evalmask,
                  char   *pname,
                  void  **pval)
{
    FILE *fp;
    char *filedir;
    char filename[FILENAME_MAX];
    char line [80];
    char name [80];
    char value[80];
    char param[80];
    char sensor[20];
    char *p;
    char *p1;
    char *p2;
    int  i;

    static int32_t  sensorID_s = -999;
    static int32_t  nbands     = 0;
    static int32_t  nbandsir   = 0;
    static int32_t  nbandsvis  = 0;

    static int32_t  bindx  [NBANDS];
    static int32_t  lam    [MAXBANDS];
    static float Fo     [NBANDS];
    static float Tau_r  [NBANDS];
    static float k_oz   [NBANDS];
    static float t_co2  [NBANDS];
    static float k_no2  [NBANDS];
    static float a_h2o  [NBANDS];
    static float b_h2o  [NBANDS];
    static float c_h2o  [NBANDS];
    static float d_h2o  [NBANDS];
    static float e_h2o  [NBANDS];
    static float f_h2o  [NBANDS];
    static float g_h2o  [NBANDS];
    static float awhite [NBANDS];
    static float aw     [NBANDS];
    static float bbw    [NBANDS];
    static float wed    [NBANDS];
    static float waph   [NBANDS];

    static float ooblw01[NBANDS];
    static float ooblw02[NBANDS];
    static float ooblw03[NBANDS];

    static float oobwv01[NBANDS];
    static float oobwv02[NBANDS];
    static float oobwv03[NBANDS];
    static float oobwv04[NBANDS];
    static float oobwv05[NBANDS];
    static float oobwv06[NBANDS];
    static float oobwv07[NBANDS];
    static float oobwv08[NBANDS];
    static float oobwv09[NBANDS];
    static float oobwv10[NBANDS];
    static float oobwv11[NBANDS];
    static float oobwv12[NBANDS];

    if (sensorID != sensorID_s) {

        strcpy(filename, g_cfg.config_f);

        strcat(filename,"/msl12_sensor_info.dat");
        if ((evalmask & ALTSENSORINFO) != 0)
            strcat(filename,".alt");

//       filename[0]='\0';
//       strcpy(filename,strcat(filename,"HY1C/msl12_sensor_info.dat"));

        if(want_verbose)
            HY1C_out("Opening sensor information file %s\n",filename);

        if ( (fp = fopen(filename,"r")) == NULL ) {
            fHY1C_out(stderr,
                    "-E- %s line %d: unable to open %s for reading\n",
                    __FILE__,__LINE__,filename);
            return(-1);
        }

        /*                                                            */
        /* Initialize output data structure                           */
        /*                                                            */
        for (i=0; i<NBANDS; i++) {
	    bindx[i] = i;        // don't delete yet
            t_co2[i] = 1.0;
            k_no2[i] = 1.0;
            a_h2o[i] = 1.0;
        }
        memset(lam,   '\0', sizeof(int32_t) *NBANDS);
        memset(Fo,    '\0', sizeof(float)*NBANDS);
        memset(Tau_r, '\0', sizeof(float)*NBANDS);
        memset(k_oz,  '\0', sizeof(float)*NBANDS);
        memset(awhite,'\0', sizeof(float)*NBANDS);
        memset(aw,    '\0', sizeof(float)*NBANDS);
        memset(bbw,   '\0', sizeof(float)*NBANDS);
        memset(wed,   '\0', sizeof(float)*NBANDS);
        memset(waph,  '\0', sizeof(float)*NBANDS);
        memset(b_h2o, '\0', sizeof(float)*NBANDS);
        memset(c_h2o, '\0', sizeof(float)*NBANDS);
        memset(d_h2o, '\0', sizeof(float)*NBANDS);
        memset(e_h2o, '\0', sizeof(float)*NBANDS);
        memset(f_h2o, '\0', sizeof(float)*NBANDS);
        memset(g_h2o, '\0', sizeof(float)*NBANDS);

        /*                                                            */
        /* Loop through each line of file looking for identifiers     */
        /*                                                            */
        while ( fgets( line, 80, fp ) ) {

            memset(name, '\0',sizeof(name ));
            memset(value,'\0',sizeof(value));

           /*
            * Skip comment lines, empty lines, and lines without a
            * name = value pair.
            */
            if (line[0] == '#' || line[0] == '\n' )
                continue;
            if ( ! (p = strchr(line,'=')) )
                continue;

           /*
            * Parse parameter name string
            */
            p1 = line;
            while (isspace(*p1))
                p1++;
            p2 = p-1;
            while (isspace(*p2))
                p2--;
            strncpy( name, p1, p2-p1+1 );

           /*
            * Parse parameter value string
            */
            p1 = p+1;
            while (isspace(*p1))
                p1++;
            p2 = p1;
            while (! isspace(*p2))
                p2++;
            strncpy( value, p1, p2-p1);

           /*
            * Copy value to appropriate variable
            */
            if (strcmp(name, "Nbands") == 0)
                nbands = (int32_t) atoi(value);
            else {
              for (i=0; i<MAXBANDS; i++) {
                sprintf(param,"Lambda(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    lam[i] = (int32_t) atoi(value);
		    break;
		}
	      }
              for (i=0; i<NBANDS; i++) {
                sprintf(param,"F0(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    Fo[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"Tau_r(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    Tau_r[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"k_oz(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    k_oz[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"t_co2(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    t_co2[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"k_no2(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    k_no2[i] = (float) atof(value);
		    break;
		}

                sprintf(param,"a_h2o(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    a_h2o[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"b_h2o(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    b_h2o[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"c_h2o(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    c_h2o[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"d_h2o(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    d_h2o[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"e_h2o(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    e_h2o[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"f_h2o(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    f_h2o[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"g_h2o(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    g_h2o[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"awhite(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    awhite[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"aw(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    aw[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"bbw(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    bbw[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"wed(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    wed[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"waph(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    waph[i] = (float) atof(value);
		    break;
		}

                sprintf(param,"ooblw01(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    ooblw01[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"ooblw02(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    ooblw02[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"ooblw03(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    ooblw03[i] = (float) atof(value);
		    break;
		}

                sprintf(param,"oobwv01(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv01[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv02(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv02[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv03(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv03[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv04(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv04[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv05(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv05[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv06(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv06[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv07(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv07[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv08(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv08[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv09(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv09[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv10(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv10[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv11(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv11[i] = (float) atof(value);
		    break;
		}
                sprintf(param,"oobwv12(%d)",i+1);
                if (strcmp(name,param) == 0) {
                    oobwv12[i] = (float) atof(value);
		    break;
		}

	      }
	    }

        }
    
        /*                                                            */
        /* Separate bands into reflected and emmissive and visible    */
        /*                                                            */
        for (i=0; i<nbands; i++)
	  if (lam[i] > MINWAVE_IR)
	    break;

        nbandsir = nbands-i;
        nbands   = i;

        for (i=0; i<nbands; i++)
	  if (lam[i] > MAXWAVE_VIS)
	    break;

        nbandsvis = i;


        /*                                                            */
        /* Write-out what was read, for informational purposes        */
        /*                                                            */
        if(want_verbose) {

            HY1C_out("  %3s %5s %8s %8s %8s %8s %8s %8s %8s %8s\n",
               "Bnd","Lam","Fo","Tau_r","k_oz","k_no2","t_co2","awhite","aw","bbw");
//            for (i=0; i<nbands; i++)
                            for (i=0; i<8; i++)
                HY1C_out("  %3d %5d %8.3f %8.3e %8.3e %8.3e %8.3e %8.3e %8.3e %8.3e\n", i,
                    lam   [i],
                    Fo    [i],
                    Tau_r [i],
                    k_oz  [i],
                    k_no2 [i],
                    t_co2 [i],
                    awhite[i],
                    aw    [i],
                    bbw   [i]);
            HY1C_out("\n");

//            if (nbandsir > 0) {
//                HY1C_out("  %3s %5s\n","Bnd","Lam");
//                for (i=0; i<nbandsir; i++)
//                    HY1C_out("  %3d %5d\n", i+nbands, lam   [i+nbands]);
//            }
            HY1C_out("\n");
        } // want_verbose
    }

    /*                                              */
    /* Return a pointer to the requested parameter  */
    /*                                              */
    if (pval != NULL) {
      if (strcmp(pname,"Nbands") == 0) 
       *pval = (void *) &nbands;
      else if (strcmp(pname,"NbandsIR") == 0) 
       *pval = (void *) &nbandsir;
      else if (strcmp(pname,"NbandsVIS") == 0) 
       *pval = (void *) &nbandsvis;
      else if (strcmp(pname,"Bindx") == 0)
       *pval = (void *) bindx;
      else if (strcmp(pname,"Lambda") == 0) 
       *pval = (void *) lam;
      else if (strcmp(pname,"Fobar") == 0) 
       *pval = (void *) Fo;
      else if (strcmp(pname,"Tau_r") == 0) 
       *pval = (void *) Tau_r;
      else if (strcmp(pname,"k_oz") == 0) 
       *pval = (void *) k_oz;
      else if (strcmp(pname,"t_co2") == 0) 
       *pval = (void *) t_co2;
      else if (strcmp(pname,"k_no2") == 0) 
       *pval = (void *) k_no2;

      else if (strcmp(pname,"a_h2o") == 0) 
       *pval = (void *) a_h2o;
      else if (strcmp(pname,"b_h2o") == 0) 
       *pval = (void *) b_h2o;
      else if (strcmp(pname,"c_h2o") == 0) 
       *pval = (void *) c_h2o;
      else if (strcmp(pname,"d_h2o") == 0) 
       *pval = (void *) d_h2o;
      else if (strcmp(pname,"e_h2o") == 0) 
       *pval = (void *) e_h2o;
      else if (strcmp(pname,"f_h2o") == 0) 
       *pval = (void *) f_h2o;
      else if (strcmp(pname,"g_h2o") == 0) 
       *pval = (void *) g_h2o;

      else if (strcmp(pname,"awhite") == 0) 
       *pval = (void *) awhite;

      else if (strcmp(pname,"aw") == 0) 
       *pval = (void *) aw;
      else if (strcmp(pname,"bbw") == 0) 
       *pval = (void *) bbw;

      else if (strcmp(pname,"wed") == 0) 
       *pval = (void *) wed;
      else if (strcmp(pname,"waph") == 0) 
       *pval = (void *) waph;

      else if (strcmp(pname,"ooblw01") == 0) 
       *pval = (void *) ooblw01;
      else if (strcmp(pname,"ooblw02") == 0) 
       *pval = (void *) ooblw02;
      else if (strcmp(pname,"ooblw03") == 0) 
       *pval = (void *) ooblw03;

      else if (strcmp(pname,"oobwv01") == 0) 
       *pval = (void *) oobwv01;
      else if (strcmp(pname,"oobwv02") == 0) 
       *pval = (void *) oobwv02;
      else if (strcmp(pname,"oobwv03") == 0) 
       *pval = (void *) oobwv03;
      else if (strcmp(pname,"oobwv04") == 0) 
       *pval = (void *) oobwv04;
      else if (strcmp(pname,"oobwv05") == 0) 
       *pval = (void *) oobwv05;
      else if (strcmp(pname,"oobwv06") == 0) 
       *pval = (void *) oobwv06;
      else if (strcmp(pname,"oobwv07") == 0) 
       *pval = (void *) oobwv07;
      else if (strcmp(pname,"oobwv08") == 0) 
       *pval = (void *) oobwv08;
      else if (strcmp(pname,"oobwv09") == 0) 
       *pval = (void *) oobwv09;
      else if (strcmp(pname,"oobwv10") == 0) 
       *pval = (void *) oobwv10;
      else if (strcmp(pname,"oobwv11") == 0) 
       *pval = (void *) oobwv11;
      else if (strcmp(pname,"oobwv12") == 0) 
       *pval = (void *) oobwv12;

      else
	return(-1);
    }

    sensorID_s = sensorID;

    if (pname != NULL) {
        if (strcmp(pname,"NbandsIR") == 0) 
            return (nbandsir);
        else if (strcmp(pname,"NbandsVIS") == 0) 
            return (nbandsvis);
        else
            return (nbands);
    } else
        return (nbands);
}

